public class WaterHeaterStateMachine {
    enum State {
        OFF, //未烧水
        ON, //烧水中
        SLEEP //休眠
    }
    private State state = State.OFF;
    //传感器量
    private double temperature = 25;//初始温度
    private boolean hasWater = true; // 初始有水
    //系统时间
    private int hour = 7;//默认从早上7点启动
    //加热器是否通电
    private boolean powerOn = false;

    public void setTemperature(double temperature) { this.temperature = temperature; }
    public void setHasWater(boolean hasWater) { this.hasWater = hasWater; }
    public void setHour(int hour) { this.hour = hour; }

    public void update() {
        switch (state) {
            case OFF:
                if (temperature < 20 && hasWater) {
                    powerOn = true;
                    state = State.ON;
                    System.out.println("状态切换：OFF → ON（温度<20 且有水）");
                } else if (temperature < 20 && !hasWater) {
                    powerOn = false;
                    System.out.println("保持 OFF（温度<20 但无水）");
                }
                break;

            case ON:
                if (temperature >= 100) {
                    powerOn = false;
                    state = State.OFF;
                    System.out.println("状态切换：ON → OFF（水温达到100°）");
                } else if (hour >= 23) {
                    powerOn = false;
                    state = State.SLEEP;
                    System.out.println("状态切换：ON → SLEEP（晚上11点）");
                }
                break;

            case SLEEP:
                if (hour == 7) {
                    state = State.OFF;
                    System.out.println("状态切换：SLEEP → OFF（早上7点）");
                }
                break;
        }
    }

    public void printStatus() {
        System.out.println("当前状态：" + state +
                ", 温度=" + temperature +
                ", 有水=" + hasWater +
                ", 时刻=" + hour +
                ", 加热器=" + (powerOn ? "通电" : "断电"));
    }
}
